﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.SessionState;

namespace Session
{
    public class MvcApplication : System.Web.HttpApplication
    {
        LogHelper.LogHelper _log = new LogHelper.LogHelper();
        /// <summary>
        /// 程序开始----隐藏事件
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            _log.WriteLine("程序开始");
        }
        /// <summary>
        /// 程序结束----隐藏事件
        /// </summary>
        protected void Application_End()
        {
            _log.WriteLine("程序结束");
        }
        /// <summary>
        /// 回话开始----隐藏事件
        /// 当用户指定浏览器第一次访问网站 创建 ASP.NET_SessionId 回话开始，
        /// 当用户关闭浏览器，再次使用相同浏览器访问网站，重新创建 ASP.NET_SessionId 新的回话开始
        /// </summary>
        protected void Session_Start()
        {
            _log.WriteLine("回话开始,count=" + Session.Count + ",回话ID：" + Session.SessionID);
            StringBuilder builder = new StringBuilder();
            foreach (string item in Session.Keys)
            {
                string line = string.Format("---{0}:{1}", item, Session[item]);
                builder.AppendLine(line);
            }
            _log.WriteLine(builder.ToString());
        }
        /// <summary>
        /// 回话结束----隐藏事件
        /// 当程序结束，清除服务器所有的回话ID，每个回话ID的删除都会触发该事件
        /// 在同一个回话ID中，只能访问当前回话中创建的Session 数据
        /// </summary>
        protected void Session_End()
        {
            _log.WriteLine("回话结束,count=" + Session.Count + ",回话ID:" + Session.SessionID);
            StringBuilder builder = new StringBuilder();
            foreach (string item in Session.Keys)
            {
                string line = string.Format("---{0}:{1}", item, Session[item]);
                builder.AppendLine(line);
            }
            _log.WriteLine(builder.ToString());
        }





        /********************请求处理顺序执行事件**********************/
        /// <summary>
        /// 请求入站
        /// 在此管道中禁止访问Session
        /// </summary>
        protected void Application_BeginRequest()
        {
            Response.Write("----BeginRequest---");
            Response.Write("<br/>");
        }
        /// <summary>
        /// 发送内容到客户端之前
        /// 此管道中禁止访问Session
        /// </summary>
        protected void Application_PreSendRequestContent()
        {
            Response.Write("----PreSendContent---:");
            Response.Write("<br/>");
        }
        /// <summary>
        /// 页面框架执行
        /// 此管道中可以访问Session
        /// </summary>
        protected void Application_PostRequestHandlerExecute()
        {
            Response.Write("----PostRequestHandlerExecute---count:" + Session.Count + ",当前SessionID:" + Session.SessionID);
            StringBuilder builder = new StringBuilder();
            Response.Write("<div style='text-indent:2em;'>");
            foreach (string item in Session.Keys)
            {
                string line = string.Format("---{0}:{1}", item, Session[item]);
                builder.AppendLine(line);
            }
            Response.Write(builder.ToString());
            Response.Write("</div>");
            Response.Write("<br/>");
        }
        /// <summary>
        /// 页面框架释放请求状态
        /// 此管道中禁止访问Session
        /// </summary>
        protected void Application_ReleaseRequestState()
        {
            Response.Write("----ReleaseRequestStatet---");
            Response.Write("<br/>");
        }
        /// <summary>
        /// 页面框架响应完成
        ///此管道，禁止使用Session
        /// </summary>
        protected void Application_UpdateRequestCache()
        {
            Response.Write("----UpdateRequestCache---");
            Response.Write("<br/>");
        }
        /// <summary>
        /// 请求出站
        /// 此管道中禁止访问Session
        /// </summary>
        protected void Application_EndRequest()
        {
            Response.Write("----EndRequest---");
            Response.Write("<br/>");
        }



    }
}
